#### Making plots in the appendix that trace development of outcome variable over time

## Init
library(directlabels)

## Define an empty theme
theme_appendix <- theme(legend.position = "none",
                        panel.background = element_rect(fill=NA), 
                        text=element_text(size=18),
                        axis.text = element_text(size = rel(1.0)),
                        panel.grid.major.y = element_blank(), 
                        panel.grid.major.x = element_blank(),
                        axis.line = element_line(size = 1, colour = "black"))

## Reshape analysis data to get distribution of outcome variables
gather_df <- analysis_df %>% 
  select(iso3c, year,
         climate_laws, ecp_zeros, elec_renew, 
         fit, quota,
         delegates, gcg, cf_principal_provided, cf_total_provided,
         cf_principal_received, cf_total_received) %>% 
  pivot_longer(names_to = "outcome", values_to = "value",
               climate_laws:cf_total_received) %>% 
  group_by(year, outcome) %>%
  summarize(quantile_10 = quantile(value, 0.1, na.rm=T),
            quantile_20 = quantile(value, 0.2, na.rm=T),
            quantile_30 = quantile(value, 0.3, na.rm=T),
            quantile_40 = quantile(value, 0.4, na.rm=T),
            quantile_50 = quantile(value, 0.5, na.rm=T),
            quantile_60 = quantile(value, 0.6, na.rm=T),
            quantile_70 = quantile(value, 0.7, na.rm=T),
            quantile_80 = quantile(value, 0.8, na.rm=T),
            quantile_90 = quantile(value, 0.9, na.rm=T)) %>% 
  arrange(outcome, year) %>% 
  pivot_longer(names_to = "measure", values_to = "value", 
               quantile_10:quantile_90) %>% 
  mutate(measure = paste(str_sub(measure, start = -2L, end = -1L),
                       "th", sep = ""))

#### Plot for each outcome

# Laws
gather_df %>% 
  filter(outcome == "climate_laws") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="Climate laws") + 
  geom_point(data = gather_df %>%
               filter(outcome == "climate_laws",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_laws.pdf", units = "in", height = 6, width = 10)

# Carbon price
gather_df %>% 
  filter(outcome == "ecp_zeros") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="Emissions-weighted carbon price") + 
  geom_point(data = gather_df %>%
               filter(outcome == "ecp_zeros",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_ecp.pdf", units = "in", height = 6, width = 10)

# Renewables
gather_df %>% 
  filter(outcome == "elec_renew") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="Renewable electricity generation") + 
  geom_point(data = gather_df %>%
               filter(outcome == "elec_renew",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_renewables.pdf", units = "in", height = 6, width = 10)

# UN climate delegates
gather_df %>% 
  filter(outcome == "delegates") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="UN climate delegates") + 
  geom_point(data = gather_df %>%
               filter(outcome == "delegates",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_delegates.pdf", units = "in", height = 6, width = 10)

# GCG
gather_df %>% 
  filter(outcome == "gcg") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="Membership in climate institutions") + 
  geom_point(data = gather_df %>%
               filter(outcome == "gcg",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_gcg.pdf", units = "in", height = 6, width = 10)

# Total provided
gather_df %>% 
  filter(outcome == "cf_total_provided") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="(Logged) Total climate finance provided") + 
  geom_point(data = gather_df %>%
               filter(outcome == "cf_total_provided",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_total_prov.pdf", units = "in", height = 6, width = 10)

# Principal provided
gather_df %>% 
  filter(outcome == "cf_principal_provided") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="(Logged) Principal climate finance provided") + 
  geom_point(data = gather_df %>%
               filter(outcome == "cf_principal_provided",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_prin_prov.pdf", units = "in", height = 6, width = 10)

# Total received
gather_df %>% 
  filter(outcome == "cf_total_received") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="(Logged) Total climate finance received") + 
  geom_point(data = gather_df %>%
               filter(outcome == "cf_total_received",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_total_received.pdf", units = "in", height = 6, width = 10)

# Principal received
gather_df %>% 
  filter(outcome == "cf_principal_received") %>% 
  ggplot(aes(year, value, group = measure, color = measure)) + 
  labs(x="Year", y="(Logged) Principal climate finance received") + 
  geom_point(data = gather_df %>%
               filter(outcome == "cf_principal_received",
                      measure == "50th"),
             aes(year, value),
             shape = 16,
             size = 3,
             color = "#456613") +
  geom_line() +
  scale_color_manual(values = c("grey90", "grey80",
                                "grey70", "grey60",
                                "#456613", "grey40",
                                "grey30", "grey20",
                                "grey10")) +
  theme_appendix +
  geom_dl(aes(label = measure), method = list(dl.combine("last.points"), cex = 1, hjust = -0.25)) 
# ggsave("text/images/outcome_prin_received.pdf", units = "in", height = 6, width = 10)

analysis_df %>% 
  select(iso3c, year, fit, quota) %>% 
  group_by(year) %>% 
  summarize(sum_fit = sum(fit, na.rm=T),
            sum_quota = sum(quota, na.rm=T)) %>% 
  filter(year<2014) %>% 
  ggplot(., aes(year, sum_fit)) + 
  geom_point(size = 2) +
  geom_line(color = "#456613") +
  labs(x="Year", y="Number of countries with feed-in tariff") + 
  theme_appendix
# ggsave("text/images/outcome_fit.pdf", units = "in", height = 6, width = 10)


analysis_df %>% 
  select(iso3c, year, fit, quota) %>% 
  group_by(year) %>% 
  summarize(sum_fit = sum(fit, na.rm=T),
            sum_quota = sum(quota, na.rm=T)) %>% 
  filter(year<2014) %>% 
  ggplot(., aes(year, sum_quota)) + 
  geom_point(size = 2) +
  geom_line(color = "#456613") +
  labs(x="Year", y="Number of countries with renewable electricity quota") + 
  theme_appendix
# ggsave("text/images/outcome_quota.pdf", units = "in", height = 6, width = 10)


